package io.light.frame.dal.mybatis.generator.core.meta.visit.opt;

import io.light.frame.dal.mybatis.generator.core.meta.Dialect;
import org.apache.commons.lang3.StringUtils;

/**
 * H2 meta operations
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-06-10 03:18
 */
public class H2_MysqlMetaOperations extends AbstractMetaOperations {

    public H2_MysqlMetaOperations(Dialect dialect) {
        super(dialect);
    }

    /**
     * Build sql for find current database
     *
     * @return current database name
     */
    @Override
    public String buildCurrentDatabaseSql() {
        return "select database()";
    }

    /**
     * Build sql for columns metadata
     *
     * @param database  database name
     * @param tableName table name
     * @return sql
     */
    @Override
    protected String buildColumnsMetaSql(String database, String tableName) {
        StringBuilder sql = new StringBuilder();
        sql.append("select *");
        sql.append(" from ").append("INFORMATION_SCHEMA.columns");
        sql.append(" where ");
        if (StringUtils.isNotBlank(database)) {
            sql.append("table_catalog=:database");
            sql.append(" and ");
        }
        sql.append("`table_name`=:tableName");
        return sql.toString();
    }

}
